﻿using DLL.NET60.DB.MS.Entity.DTO;
using DLL.NetStandard.FlushRobot.MS.Entity.Param;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Windows;

namespace DLL.FlushRobot.MS.Run
{
    public class COneLoopStations : ICloneable
    {
        private static int MaxRowPointCount = 8;
        private static int MaxColPointCount = 8;
        public int MaxDrugCount = 0;
        public int LoopIndex = 0;
        public DTO_adi_dic_drug UsedDrug = new DTO_adi_dic_drug();
        public DTO_adi_dic_drug UsedSolvent = new DTO_adi_dic_drug();
        public CMS_DeviceConfigParam DeviceConfig = new CMS_DeviceConfigParam();//设备参数
        public COneRowStation[] MultyRowStation = new COneRowStation[MaxRowPointCount];
        public int StationColNum = 6;

        public COneLoopStations()
        {
            UsedDrug.InitializeFlushGroup();
            UsedDrug.InitializeFlushParam();
            UsedSolvent.InitializeFlushGroup();
            UsedSolvent.InitializeFlushParam();
        }

        public object Clone()
        {
            COneLoopStations oneLoop = new COneLoopStations();
            oneLoop.MaxDrugCount = this.MaxDrugCount;
            oneLoop.LoopIndex = this.LoopIndex;
            oneLoop.StationColNum = this.StationColNum;
            oneLoop.UsedSolvent = (DTO_adi_dic_drug)this.UsedSolvent.Clone();
            oneLoop.UsedDrug = (DTO_adi_dic_drug)this.UsedDrug.Clone();
            oneLoop.DeviceConfig = this.DeviceConfig;
            for (int i = 0; i < MaxRowPointCount; i++)
            {
                oneLoop.MultyRowStation[i] = (COneRowStation)this.MultyRowStation[i].Clone();
            }
            return oneLoop;
        }

        //获取配药循环测试
        public int[] GetDispenseMedicinesLoopCount(int row = 1)
        {
            int[] values = new int[6];
            for (int i = 0; i < StationColNum; i++)
            {
                int loopCount = 0;
                if (MultyRowStation[1].RowStations[i].UsedFlag == 1)
                {
                    int FlushGroupIndex = MultyRowStation[1].RowStations[i].UsedDrug.FlushGroupIndex;
                    loopCount = MultyRowStation[row].RowStations[i].UsedDrug.flush_Groups[FlushGroupIndex].FlushLoopCount + 1;
                }
                else
                {
                    loopCount = 0;
                }
                values[i] = loopCount;
            }
            return values;
        }

        public int GetMaxDispenseMedicinesLoop(int row = 1)
        {
            int[] values = GetDispenseMedicinesLoopCount(row);
            int value = GetMaxValue(values);
            return value;
        }

        //获取回抽循环次数
        //PumpBackLoopCount
        public int[] GetPumpBackLoopCount(int row = 1)
        {
            return MultyRowStation[row].GetPumpBackLoopCount();
        }

        public int GetMaxPumpBackLoopCount(int row = 1)
        {
            int[] values = GetPumpBackLoopCount(row);
            int value = GetMaxValue(values);
            return value;
        }

        //抽一次配几支药
        public int[] GetDrugCountOfOneLoop(int loopIndex, int row = 1)
        {
            int[] values = MultyRowStation[row].GetDrugCountOfLoopFlush(loopIndex);
            return values;
        }

        public int GetMinDrugCountOfOneLoop(int loopIndex, int[] enableLoopFlag, int row = 1)
        {
            int[] values = MultyRowStation[row].GetDrugCountOfLoopFlush(loopIndex);//获取抽一次溶媒配几支药
            for (int col = 0; col < values.Length; col++)
            {
                if (enableLoopFlag[col] == 0)
                {
                    values[col] = -1;
                }
            }
            int value = GetMinDrugCountOfOneFlush(values, enableLoopFlag);
            return value;
        }

        public int GetMinDrugCountOfOneFlush(int[] values, int[] enableLoopFlag)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int col = 0; col < values.Length; col++)
                {
                    switch (i)
                    {
                        case 0:
                            {
                                if ((enableLoopFlag[col] == 1) && (values[col] == 1))
                                {
                                    return 1;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        case 1:
                            {
                                if ((enableLoopFlag[col] == 1) && (values[col] == 2))
                                {
                                    return 2;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        case 2:
                            {
                                if ((enableLoopFlag[col] == 1) && (values[col] == 4))
                                {
                                    return 4;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        default:
                            break;
                    }
                }
            }
            return 1;
        }

        //public double GetSolventVolume(int loopIndex)
        //{
        //    int FlushGroupIndex = GetFlushGroupIndex();
        //    return UsedDrug.flush_Groups[FlushGroupIndex].flush_Params[loopIndex].SolventVolume;

        //}
        //public double GetAddtionalPumping(int loopIndex)
        //{
        //    int FlushGroupIndex = GetFlushGroupIndex();
        //    return UsedDrug.flush_Groups[FlushGroupIndex].flush_Params[loopIndex].ADDITIONAL_PUMPING;
        //}
        //public int GetFlushGroupIndex()
        //{
        //    int FlushGroupIndex = MultyRowStation[1].RowStations[0].UsedDrug.FlushGroupIndex;
        //    return FlushGroupIndex;
        //}
        public int GetMaxValue(int[] values)
        {
            int max = 0;
            for (int i = 0; i < StationColNum; i++)
            {
                if (values[i] > max)
                {
                    max = values[i];
                }
            }
            return max;
        }

        public int GetMinValue(int[] values)
        {
            int min = values[0];
            for (int i = 0; i < StationColNum; i++)
            {
                if (values[i] == -1)
                {
                    continue;
                }
                if (values[i] < min)
                {
                    min = values[i];
                }
                else
                {
                    if (min == -1)
                    {
                        min = values[i];
                    }
                }
            }
            return min;
        }

        public double GetMaxValue(double[] all)
        {
            int count = all.Length;
            double a = 0;
            for (int i = 0; i < count; i++)
            {
                if (all[i] > a)
                {
                    a = all[i];
                }
            }
            return a;
        }

        //1.初始正压回抽空气,只有正压和负压之分
        public int[] GetOrgPressure(int row = 1)
        {
            //获取原始压强
            int[] org = new int[6] { 0, 0, 0, 0, 0, 0 };
            COneRowStation oneRow = MultyRowStation[row];
            for (int col = 0; col < StationColNum; col++)
            {
                if (oneRow.RowStations[col].UsedFlag == 1)
                {
                    org[col] = oneRow.RowStations[col].UsedDrug.ORIGINAL_PRESSURE;
                }
                else
                {
                    org[col] = -1;
                }
            }
            return org;
        }
    }
}